<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('用户名称')" prop="userId">
            <UserSelect v-model="form.userId" :placeholder="$t('用户名称')" :disabled="form.id !== undefined" />
          </el-form-item>
        </el-col>
        <!-- 基金成员 -->
        <el-col :span="12">
          <el-form-item :label="$t('参与角色')" prop="role">
            <el-select v-model="form.role" :placeholder="$t('参与角色')">
              <el-option
                v-for="dict in roleOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('参与时间')" prop="joinTime">
            <DatePicker
              v-model="form.joinTime"
              :placeholder="$t('参与时间')"
            />

          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否离开')">
            <el-radio-group v-model="form.leave" @change="leaveChange">
              <el-radio
                v-for="dict in leaveOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="isShowLeaveTime" :span="12">
          <el-form-item :label="$t('离开时间')" prop="leaveTime">
            <DatePicker
              v-model="form.leaveTime"
              :placeholder="$t('离开时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注信息')" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              maxlength="2000"
              show-word-limit
              :rows="2"
              :placeholder="$t('备注信息')"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-show="!btnHide" slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="fund:team" />
    </div>
  </div>
</template>

<script>
    import { saveFundTeam, getFundTeam } from '@/api/fund/team/fundTeam'
    export default {
        data() {
            return {
                // 参与角色 字典fund_team_role字典
                roleOptions: [], // 是否离开 字典 sys_yes_no字典
                leaveOptions: [], // 表单参数
                // 是否展示离开时间
                isShowLeaveTime: false,
                form: {},
                // 遮罩层
                loading: false,
                // 是否隐藏保存等按钮
                btnHide: false,
                // 表单校验
                rules: {
                  userId: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ], role: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ], joinTime: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ], leaveTime: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ]
                }
            }
        },
        created() {
            this.getDicts('fund_team_role').then(response => {
                this.roleOptions = response.data
            })
            this.getDicts('sys_yes_no').then(response => {
                this.leaveOptions = response.data
            })
          this.$bus.$emit('async_done')
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getFundTeam(data.id).then(response => {
                        this.form = response.data
                        this.leaveChange(this.form.leave)
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.leaveChange(this.form.leave)
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                userId: undefined,
                fundId: this.$route.query.id,
                role: undefined,
                joinTime: new Date(),
                leave: 'N',
                leaveTime: undefined,
                remark: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveFundTeam(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                                this.reset()
                            }
                            this.loading = false
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          leaveChange(value) {
            if (value === 'Y') {
              this.isShowLeaveTime = true
            } else {
              this.isShowLeaveTime = false
              this.form.leaveTime = undefined
            }
          }

        }

    }
</script>
